import csv

class Contact:
    def __init__(self, name, phone, QQ_num, email):
        self.name = name
        self.phone = phone
        self.QQ_num = QQ_num
        self.email = email

    def __str__(self):   # 重写__str__方法，使得print(contact)输出更加友好
        return f"Name: {self.name}, Phone: {self.phone}, QQ: {self.QQ_num}, Email: {self.email}"

class AddressContact:
    def __init__(self):
        self.contacts = {}

    def load_contacts(self, file_name):
        with open(file_name, 'r', newline='') as file:
            reader = csv.reader(file)
            for row in reader:
                name, phone, QQ_num, email = row
                self.contacts[name] = Contact(name, phone, QQ_num, email)

    def save_contacts(self, file_name):
        with open(file_name, 'w', newline='') as file:
            writer = csv.writer(file)
            for contact in self.contacts.values():
                writer.writerow([contact.name, contact.phone, contact.QQ_num, contact.email])

    def add_contact(self, name, phone, QQ_num, email):
        new_contact = Contact(name, phone, QQ_num, email)
        self.contacts[name] = new_contact

    def remove_contact(self, name):
        if name in self.contacts:
            del self.contacts[name]
        else:
            print("Contact not found.")

    def find_contact(self, name):
        if name in self.contacts:
            return self.contacts[name]
        else:
            return "Contact not found."

    def update_contact(self, name, new_phone, new_QQ_num, new_email):
        if name in self.contacts:
            self.contacts[name].phone = new_phone
            self.contacts[name].QQ_num = new_QQ_num
            self.contacts[name].email = new_email
        else:
            print("Contact not found.")

    def display_all_contacts(self):
        for contact in self.contacts.values():
            print(contact)

def print_menu():
    print("欢迎使用通讯录管理系统")
    print("1. 添加联系人")
    print("2. 查看通讯录")
    print("3. 删除联系人")
    print("4. 修改联系人信息")
    print("5. 查找联系人")
    print("6. 退出系统")
    print("**************************")

def main():
    address_book = AddressContact()
    address_book.load_contacts('contacts.csv')

    while True:
        print_menu()
        choice = input("请输入功能对应的数字： ")

        if choice == '1':
            name = input("请输入姓名: ")
            phone = input("请输入电话号码: ")
            QQ_num = input("请输入QQ号码: ")
            email = input("请输入邮箱: ")
            address_book.add_contact(name, phone, QQ_num, email)
            address_book.save_contacts('contacts.csv')
            print("联系人添加成功.")

        elif choice == '2':
            address_book.display_all_contacts()

        elif choice == '3':
            name = input("请输入要删除的联系人姓名: ")
            address_book.remove_contact(name)
            address_book.save_contacts('contacts.csv')
            print("联系人删除成功.")

        elif choice == '4':
            name = input("请输入要修改信息的联系人姓名: ")
            new_phone = input("请输入新的电话号码: ")
            new_QQ_num = input("请输入新的QQ号码: ")
            new_email = input("请输入新的邮箱: ")
            address_book.update_contact(name, new_phone, new_QQ_num, new_email)
            address_book.save_contacts('contacts.csv')
            print("联系人信息已更新.")

        elif choice == '5':
            name = input("请输入要查找的联系人姓名: ")
            contact = address_book.find_contact(name)
            print(contact)

        elif choice == '6':
            break

        else:
            print("无效选项，请重试.")

if __name__ == '__main__':
    main()